<template>

<div id="updateShareCode">
  <div id="homeBar">
    <mu-appbar title="修改邀请码" style="background-color:#409EFF">
      <mu-icon-button  icon="arrow_back_ios" slot="left" :to="{name: 'Home' }"/>
    </mu-appbar>
  </div>

  <el-row>
    <el-col :span="12">
    </el-col>
    <el-col :span="12">
      <i class="el-icon-arrow-down" style="float:right" @click="handleTools"></i>
    </el-col>
  </el-row>

  <div class= "tools" v-show="tools">
    <el-row>
      <el-col :xs="6" :sm="4" :md="3" :lg="3">
        玩家ID
      </el-col>
      <el-col :xs="18" :sm="20" :md="21" :lg="21">
        <el-input v-model="condition.uid" size="mini" @blur="getUsers"></el-input>
      </el-col>
  </el-row>
      <!-- <el-col :xs="24" :sm="8" :md="8" :lg="8">
        <el-col :xs="6" :sm="6" :md="6" :lg="6">
          起始日期
        </el-col>
        <el-col :xs="18" :sm="18" :md="18" :lg="18">
          <el-date-picker
            v-model="condition.startDay"
            type="date"
            placeholder="选择起始日期"
            size="mini"
            @change="getUsers"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-col>
      </el-col>

      <el-col :xs="24" :sm="8" :md="8" :lg="8">
        <el-col :xs="6" :sm="6" :md="6" :lg="6">
          结束日期
        </el-col>
        <el-col :xs="18" :sm="18" :md="18" :lg="18">
          <el-date-picker
            v-model="condition.endDay"
            type="date"
            placeholder="选择结束日期"
            size="mini"
            @change="getUsers"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-col>
      </el-col> -->
  </div>

  <el-dialog title="修改邀请码" :visible.sync="dialogFormVisible">
    <el-form :model="form">
      <el-form-item label="玩家ID">
        <el-input v-model="form.uid" size="mini" disabled></el-input>
      </el-form-item>
      <el-form-item label="新绑定者ID">
        <el-input v-model="form.binderUid" size="mini"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </el-dialog>

  <el-row>
    <el-table
      :data="tableData"
      style="width: 100%"
      size="mini">
      <el-table-column
        prop="uid"
        label="玩家ID">
      </el-table-column>
      <el-table-column
        prop="binderUid"
        label="上级ID">
      </el-table-column>
      <el-table-column
        prop="nickName"
        label="昵称">
      </el-table-column>
      <el-table-column
        prop="ndot"
        label="当前金币">
        <template slot-scope="scope">
          <span>{{scope.row.ndot}}个</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="isAgent"
        label="是否代理">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="注册时间">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleUpdate(scope.row)">修改邀请码</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-row>

  <el-row>
    <center>
      <el-pagination
        small
        layout="prev, pager, next"
        :total="condition.total"
        :page-size="condition.pageSize"
        :current-page="condition.currentPage"
        @current-change="handleCurrentChange">
      </el-pagination>
    </center>
  </el-row>
</div>

</template>

<style>
#updateShareCode .el-dialog {
  width: 70%
}
#updateShareCode .el-row {
    margin-bottom: 16px;
  }
#updateShareCode .el-form-item__content {
    margin-bottom: -24px
  }
#updateShareCode form.el-form {
    margin-top: -24px;
  }
</style>

<script>
  import moment from 'moment'
  export default {
    data () {
      return {
        form: {
          uid: '',
          binderUid: '',
          account: sessionStorage.getItem('account')
        },
        condition: {
          startDay: '',
          endDay: '',
          uid: '',
          currentPage: 1,
          pageSize: 10,
          total: 0,
          account: sessionStorage.getItem('account')
        },
        dialogFormVisible: false,
        tools: true,
        tableData: []
      }
    },
    methods: {
      validate () {
        for (let item in this.form) {
          if (this.form[item].length === 0) {
            this.$message.error('请完善信息')
            return false
          }
        }
        return true
      },
      handleTools () {
        if (this.tools) {
          this.tools = false
        } else {
          this.tools = true
        }
      },
      init () {
        this.getUsers()
      },
      getUsers () {
        let that = this
        if (this.condition.uid === '' || this.condition.uid.length === 0) {
          this.condition.uid = -1
        }
        this.$axios({
          method: 'get',
          url: '/api/AgentService.getUsersForUpdateShareCode',
          params: this.condition
        })
        .then(function (resp) {
          that.condition.uid = ''
          if (resp.data.status === 'success') {
            for (let item in resp.data.data) {
              resp.data.data[item].createTime = moment.utc(resp.data.data[item].createTime).zone(-8).format('YYYY-MM-DD HH:mm:ss')
            }
            that.tableData = resp.data.data
            that.condition.total = resp.data.page.total
          }
        })
      },
      handleCurrentChange (val) {
        this.condition.currentPage = val
        this.getUsers()
      },
      handleUpdate (row) {
        this.dialogFormVisible = true
        this.form.uid = row.uid
      },
      handleSubmit () {
        let that = this
        if (this.validate()) {
          this.$axios({
            method: 'get',
            url: '/api/CarrieroperatorService.updateBinderUid',
            params: this.form
          })
          .then(function (resp) {
            if (resp.data.status === 'success') {
              that.$message({
                showClose: true,
                message: '修改成功',
                type: 'success'
              })
              that.dialogFormVisible = false
              that.init()
            } else {
              that.$message({
                showClose: true,
                message: resp.data.cause,
                type: 'warning'
              })
            }
          })
        }
      }
    },
    mounted () {
      this.init()
    }
  }
</script>
